package com.bk.service.balance;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bk.entity.AccountBalance;
import com.bk.entity.CardInfo;
import com.bk.entity.TranFlowInfo;
import com.bk.entity.UserInfo;
import com.bk.mapper.balance.BalanceMapper;
import com.bk.mapper.cardInfo.CardInfoMapper;
import com.bk.mapper.userInfo.UserInfoMapper;
import com.bk.util.GeneratorRandomUtil;
import com.bk.util.Md5Util;
import com.bk.util.ReturnUtil;
import com.bk.util.TimeUtil;
import com.bk.vo.UserCardInfoVo;

@Service /** 金额有关service */
public class BalanceService {

	
	@Autowired
	private BalanceMapper balanceMapper;
	
	@Autowired
	private CardInfoMapper cardInfoMapper;
	
	@Autowired
	private UserInfoMapper userInfoMapper;

	
	/** 查询账户余额 */
	public ReturnUtil<AccountBalance> queryBalance(String accountId, String cardPassword) {
		
		CardInfo cardInfo=cardInfoMapper.isExistCardByAccountIdAndPass(accountId,Md5Util.getMd5(cardPassword));//通过账号和密码查询是否有该用户
		if(null!=cardInfo&&0==cardInfo.getCardLock()) {
			AccountBalance ab = balanceMapper.queryBalanceByAccountId(accountId);//查询当前账户的余额
			if (null != ab) {
				List<AccountBalance> accountB = new ArrayList<>();
				accountB.add(ab);
				return new ReturnUtil<AccountBalance>(200, "成功查询账户余额", accountB);
			} else {
				return new ReturnUtil<AccountBalance>(400, "该账户不存在,请正确输入账户密码", null);
			}
		}else if(1==cardInfo.getCardLock()) {
			return new ReturnUtil<AccountBalance>(400,"该账户已被冻结,请前往解冻",null);
		}else {
			return new ReturnUtil<AccountBalance>(400,"该账户不存在,请正确输入账户密码",null);
		}
		
		
	}
	
	/**存钱or取钱  通过flag确定存钱还是取钱*/
	public ReturnUtil<AccountBalance> saveOrGetBalance(int flag,CardInfo cardInfo, AccountBalance accountBalance,
			UserInfo userInfo) { 
		int returnCode=0;
		List<AccountBalance> accountL = new ArrayList<>();
		String md5Pass = Md5Util.getMd5(cardInfo.getCardPassword());
		UserCardInfoVo userCardInfoVo=cardInfoMapper.isExistCardByAccountIdAndPassAndName(cardInfo.getCardId(),md5Pass,userInfo.getUname());//通过账户和密码以及账户名判断是否存在
		CardInfo cardInfoFromDataBase=cardInfoMapper.isExistCardByAccountIdAndPass(cardInfo.getCardId(),md5Pass);//通过账号和密码查询是否有该用户
		if(null!=userCardInfoVo&&0==cardInfoFromDataBase.getCardLock()) {
			if(1==flag) {
				AccountBalance abFromDataBase = balanceMapper.queryBalanceByAccountId(accountBalance.getCardId());//查询当前账户的余额
				double balance=abFromDataBase.getBalance()+accountBalance.getBalance();//最终金额
				abFromDataBase.setBalance(balance);
				returnCode=balanceMapper.saveOrGetBalanceByCardId(abFromDataBase);//存款
				accountL.add(abFromDataBase);
				if(returnCode>0) {
					//设置流水信息
					TranFlowInfo tranInfo=new TranFlowInfo(GeneratorRandomUtil.getRandom(10),abFromDataBase.getCardId(),abFromDataBase.getCardId(),accountBalance.getBalance(),TimeUtil.getCurrentTimeUtil(),userInfo.getUname()+"用户向"+abFromDataBase.getCardId()+"账户存款"+balance+"元",1);
					balanceMapper.writeTranFlowInfo(tranInfo);//记录存款流水
				}		
				return returnCode>0 ? new ReturnUtil<AccountBalance>(200,"向"+userInfo.getUname()+"账户存款"+accountBalance.getBalance()+"元",accountL):new ReturnUtil<AccountBalance>(400,"向"+userInfo.getUname()+"账户存款失败",null);
			}else{
				AccountBalance abFromDataBase = balanceMapper.queryBalanceByAccountId(accountBalance.getCardId());//查询当前账户的余额
				double balance=abFromDataBase.getBalance()-accountBalance.getBalance();//最终金额
				if(balance>=0) {
					abFromDataBase.setBalance(balance);
					returnCode=balanceMapper.saveOrGetBalanceByCardId(abFromDataBase);//取款	
					accountL.add(abFromDataBase);
					if(returnCode>0) {
						//设置流水信息
						TranFlowInfo tranInfo=new TranFlowInfo(GeneratorRandomUtil.getRandom(10),abFromDataBase.getCardId(),abFromDataBase.getCardId(),accountBalance.getBalance(),TimeUtil.getCurrentTimeUtil(),userInfo.getUname()+"用户从"+abFromDataBase.getCardId()+"账户取款"+balance+"元",2);
						balanceMapper.writeTranFlowInfo(tranInfo);//记录取款流水
					}
					return returnCode>0 ? new ReturnUtil<AccountBalance>(200,"从"+userInfo.getUname()+"账户取款"+accountBalance.getBalance()+"元",accountL):new ReturnUtil<AccountBalance>(400,"从"+userInfo.getUname()+"账户取款失败",null);
				}else {
					return new ReturnUtil<AccountBalance>(400,"余额不足，当前账户余额为"+abFromDataBase.getBalance(),null);
				}
				
			}
		}else if(1==cardInfoFromDataBase.getCardLock()) {
			return new ReturnUtil<AccountBalance>(400,"该账户已被冻结,请前往解冻",null);
		}else {
			return new ReturnUtil<AccountBalance>(400,"操作失败，请验证账户信息与密码",null);
		}
	}

	/**A to B转账*/
	public ReturnUtil<TranFlowInfo> transferFromAToB(String sendAccount, String sendAccountName, String password,
			String recevieAccount, String recevieAccountName, String tranBalance) {
		List<AccountBalance> accountL = new ArrayList<>();
		String md5Pass = Md5Util.getMd5(password);
		double balance=Double.parseDouble(tranBalance);
		double balanceA=0;
		double balanceB=0;
		AccountBalance ADataBase =new AccountBalance();//汇款方
		AccountBalance BDataBase =new AccountBalance();//收款方
		ADataBase.setCardId(sendAccount);
		BDataBase.setCardId(recevieAccount);
		int returnCodeA=0;
		int returnCodeB=0;
		UserCardInfoVo userCardInfoVo=cardInfoMapper.isExistCardByAccountIdAndPassAndName(sendAccount,md5Pass,sendAccountName);//通过账户和密码以及账户名判断是否存在
		CardInfo cardInfoFromDataBase=cardInfoMapper.isExistCardByAccountIdAndPass(sendAccount,md5Pass);//通过账号和密码查询是否有该用户
		CardInfo reCardInfoFromDataBase=cardInfoMapper.isExistCardByAccountIdAndUname(recevieAccount,recevieAccountName);//收款方
		if(null!=userCardInfoVo&&null!=cardInfoFromDataBase&&null!=reCardInfoFromDataBase) {
			if(1==cardInfoFromDataBase.getCardLock()) {
				return new ReturnUtil<TranFlowInfo>(400,"汇款人账户被冻结，请前往解冻",null);
			}else if(1==reCardInfoFromDataBase.getCardLock()){
				return new ReturnUtil<TranFlowInfo>(400,"收款人账户被冻结，请前往解冻",null);
			}
			ADataBase = balanceMapper.queryBalanceByAccountId(sendAccount);//查询汇款账户的余额
			BDataBase = balanceMapper.queryBalanceByAccountId(recevieAccount);//查询收款账户的余额
			balanceA=ADataBase.getBalance()-balance;
			if(balanceA>0) {
				ADataBase.setBalance(balanceA);//设置汇款方金额	
				balanceB=BDataBase.getBalance()+balance;
				BDataBase.setBalance(balanceB);//设置收款方金额
				
				returnCodeA=balanceMapper.saveOrGetBalanceByCardId(ADataBase);//汇款账户扣钱
				returnCodeB=balanceMapper.saveOrGetBalanceByCardId(BDataBase);//收款方加钱
				
				if(returnCodeA>0&&returnCodeB>0) {
					//设置流水信息
					TranFlowInfo tranInfo=new TranFlowInfo(GeneratorRandomUtil.getRandom(10),ADataBase.getCardId(),BDataBase.getCardId(),balance,TimeUtil.getCurrentTimeUtil(),ADataBase.getCardId()+"向"+BDataBase.getCardId()+"汇款"+balance+"元",3);
					balanceMapper.writeTranFlowInfo(tranInfo);//记录流水
					return new ReturnUtil<TranFlowInfo>(200,ADataBase.getCardId()+"向"+BDataBase.getCardId()+"汇款"+balance+"元",null);
				}else {
					return new ReturnUtil<TranFlowInfo>(400,"汇款失败",null);
				}
			}else {
				return new ReturnUtil<TranFlowInfo>(400,"余额不足，当前汇款账户余额为"+ADataBase.getBalance()+"元",null);
			}
		}else if(null==reCardInfoFromDataBase) {
			return new ReturnUtil<TranFlowInfo>(400,"收款人账户信息有误，请重新输入",null);
		}else if(null==cardInfoFromDataBase) {
			return new ReturnUtil<TranFlowInfo>(400,"汇款人账户信息有误，请重新输入",null);
		}else {
			return new ReturnUtil<TranFlowInfo>(400,"请检查汇款账户信息与密码",null);
		}
	}
	
}
